#ifndef MOD_FORWARDSERVER
#define MOD_FORWARDSERVER

#include "STM32_Uart.h"
#include "Drv_W5500.h"
#include "Mod_Circle_buffer.h"
#include "Mod_NetConfig.h"
#include <string.h>

#define BUF_SIZE 2048
#define TCP_RECONNECT_TIMEOUT  10000  // 10s无数据则重连 
#define SOCK_RECONNECT_TIMEOUT 5000   //  5s检测socket状态

//串口序号
typedef enum
{
    UART_0 = 0x00U,
    UART_1,
    UART_2,
    UART_3,
    UART_4,
    UART_6
}SCINumber_TypeDef;

#ifdef __cplusplus

class cForwardServer
{
public:
    cForwardServer();
    ~cForwardServer(){}

    void Init(void);
    void Process(void);
    void TCPToUart(void);
    void UartToTCP(void);
	  void HeartToSocket(void);
	  void CheckNetwork(void);
	  void Close(void);
	  void SetNetConfig();
    void W5500_CheckReconnect(W5500_TypeDef *pst_EthStruct, uint8_t uch_Socket, 
                              uint8_t *puch_ServerIP, uint16_t uin_ServerPort);

    friend void UartRxCplCallback(struct _Uart_TypeDef *pst_UartStruct, uint16_t uin_Size);
    friend void UartTxCplCallback(struct _Uart_TypeDef *pst_UartStruct);
    friend void UartErrorCallback(struct _Uart_TypeDef *pst_UartStruct);

private:
    W5500_TypeDef *pst_W5500Struct;        /*W5500结构体*/
    Uart_TypeDef *pst_Socket0Uart;         /*Socket0映射的串口*/  
    Uart_TypeDef *pst_Socket1Uart;         /*Socket1映射的串口*/
    Uart_TypeDef *pst_Socket2Uart;         /*Socket2映射的串口*/
    Uart_TypeDef *pst_Socket3Uart;         /*Socket3映射的串口*/

    uint8_t auch_Socket0RxBuff[BUF_SIZE];  /*Socket0接收缓存*/
    uint8_t auch_Socket0TxBuff[BUF_SIZE];  /*Socket0发送缓存*/
    uint8_t auch_Socket1RxBuff[BUF_SIZE];  /*Socket1接收缓存*/
    uint8_t auch_Socket1TxBuff[BUF_SIZE];  /*Socket1发送缓存*/
    uint8_t auch_Socket2RxBuff[BUF_SIZE];  /*Socket2接收缓存*/
    uint8_t auch_Socket2TxBuff[BUF_SIZE];  /*Socket2发送缓存*/
    uint8_t auch_Socket3RxBuff[BUF_SIZE];  /*Socket3接收缓存*/
    uint8_t auch_Socket3TxBuff[BUF_SIZE];  /*Socket3发送缓存*/
    uint8_t auch_Socket5RxBuff[BUF_SIZE];  /*Socket5接收缓存*/
		uint8_t auch_Socket5TxBuff[2];  /*Socket5发送缓存 用来保存心跳字节*/

    uint16_t uin_Socket0RxSize;            /*Socket0接收字节数*/
    uint16_t uin_Socket0TxSize;            /*Socket0发送字节数*/
    uint16_t uin_Socket1RxSize;            /*Socket1接收字节数*/
    uint16_t uin_Socket1TxSize;            /*Socket1发送字节数*/
    uint16_t uin_Socket2RxSize;            /*Socket2接收字节数*/
    uint16_t uin_Socket2TxSize;            /*Socket2发送字节数*/
    uint16_t uin_Socket3RxSize;            /*Socket3接收字节数*/
    uint16_t uin_Socket3TxSize;            /*Socket3发送字节数*/
    uint16_t uin_Socket5RxSize;            /*Socket5接收字节数*/
    uint16_t uin_Socket5TxSize;            /*Socket5发送字节数*/		
 		

    uint8_t UartToSocket0Flag;             /*串口有数据通过Socket0发送标志位*/
    uint8_t UartToSocket1Flag;             /*串口有数据通过Socket1发送标志位*/
    //uint8_t UartToSocket2Flag;             /*串口有数据通过Socket2发送标志位*/ 
    //uint8_t UartToSocket3Flag;             /*串口有数据通过Socket3发送标志位*/
		
    		
    //新增用来存放串口收到的数据 由DMA存入
    uint8_t auch_Uart0RxBuff[BUF_SIZE];  /*Uart0接收缓存*/
    uint8_t auch_Uart1RxBuff[BUF_SIZE];  /*Uart1接收缓存*/
    uint8_t auch_Uart2RxBuff[BUF_SIZE];  /*Uart2接收缓存*/
    uint8_t auch_Uart3RxBuff[BUF_SIZE];  /*Uart3接收缓存*/

	  /* 接收环形缓冲区结构体--新增部分 */
    circle_buf Uart0RxCircleBuf;
    circle_buf Uart1RxCircleBuf;
    circle_buf Uart2RxCircleBuf;
    circle_buf Uart3RxCircleBuf;   

    /*新增环形缓冲区缓存 --新增部分 */
    uint8_t auch_Uart0RxCircleBuff[BUF_SIZE];
    uint8_t auch_Uart1RxCircleBuff[BUF_SIZE];
    uint8_t auch_Uart2RxCircleBuff[BUF_SIZE];
    uint8_t auch_Uart3RxCircleBuff[BUF_SIZE];

    
    /*新增client模式下 服务器IP地址和端口号,默认设置,全局变量开机时初始化*/
    uint8_t auch_Sock_ServerIP[4] = {192, 168, 58, 253};
    uint16_t uin_Sock0ServerPort = 40004;
    uint16_t uin_Sock1ServerPort = 40004;
    uint16_t uin_Sock2ServerPort = 40004;
    uint16_t uin_Sock3ServerPort = 40004;
		uint16_t uin_Sock4ServerPort = 40004;
    uint16_t uin_Sock5ServerPort = 40004;	//增加sock5端口号
		
		//本地端口号
//		uint16_t uin_Sock0LocalPort = 40000;
//    uint16_t uin_Sock1LocalPort = 40001;
//    uint16_t uin_Sock2LocalPort = 40002;
//    uint16_t uin_Sock3LocalPort = 40003;
//		uint16_t uin_Sock4LocalPort = 40004;
//    uint16_t uin_Sock5LocalPort = 40005;	
		
		
		uint32_t lastHeartbeatTick_Socket0;  //心跳时间戳
		uint32_t lastHeartbeatTick_Socket1;  //心跳时间戳
		uint32_t lastHeartbeatTick_Socket2;
		uint32_t lastHeartbeatTick_Socket3;
		uint32_t lastHeartbeatTick_Socket5;  //心跳时间戳
		
		
};


extern cForwardServer ForwardServer;

void UartRxCplCallback(struct _Uart_TypeDef *pst_UartStruct, uint16_t uin_Size);
void UartTxCplCallback(struct _Uart_TypeDef *pst_UartStruct);
void UartErrorCallback(struct _Uart_TypeDef *pst_UartStruct);
//uint32_t CircleBufDataAvailable(circle_buf *buf);
//uint32_t circle_buf_read_bulk(p_circle_buf pCircleBuf, uint8_t *dest, uint32_t max_len) ;

void Drv_W5500_SendFromRingBuffer(W5500_TypeDef *pst_W5500Struct,
                                  circle_buf *p_RingBuf,
                                  uint8_t *p_TxBuff,
                                  uint16_t *p_TxSize,
                                  uint8_t uch_SocketReg);

void TCP_ProcessCommand(uint8_t *rxBuffer, uint16_t len);

#endif

#endif